import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Api } from '../../../providers/api-service';
import { UrlService } from '../../../providers/url-service';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../../tabs/tabs';
import { MyJPushService } from '../../../providers/plugin-service/jPush-service';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';



@Component({
  selector: 'page-phone-bind',
  templateUrl: 'phone-bind.html',
})
export class PhoneBindPage {
  showPwd = false;//隐藏密码
  params;
  chakanPwd = true;
  chakanPwd2 = true;
  password1;
  password2;
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public api: Api,
    public storage: Storage,
    public url: UrlService,
    public loadingCtrl: LoadingController,
    public myJPushService: MyJPushService
  ) {
    this.params = navParams.get("params");
  }
  code;
  mobile;
  ionViewDidLoad() {

  }

  /**
   * 提交
   */
  loginNow() {
    if (this.mobile == "") {

      this.api.showNoticeByToast(1, "手机号不能为空！");
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(this.mobile))) {
      this.api.showNoticeByToast(1, "请输入正确的格式手机号码！");
      return false;
    }
    if (this.code == "") {
      this.api.showNoticeByToast(1, "验证码不能为空！");
      return false;
    }
    if (this.showPwd) {
      this.registerUser();
      return false;
    }
    //验证手机号
    this.api.postBody("V1/CheckPhoneNumber", {
      "PhoneNumber": this.mobile
    })
      .then(data => {
        if (data.Code == 0) {
          this.loading = this.loadingCtrl.create({
            content: '登陆中...'
          });
          this.loading.present();
          this.api.postBody("V1/ThirdBing", {
            "PhoneNumber": this.mobile,
            "Code": this.code,
            "Type": this.params.Type,
            "Context": this.params.openid
          })
            .then(data => {
              if (data.Code == 0) {
                this.loginSucess(data);
                // var obj = data.Data;
                // var str = obj.LoginTicket;
                // appcan.locStorage.setVal("Ticket", str);
                // appcan.locStorage.setVal("User", JSON.stringify(obj.User));
                // setPush(obj.User.PushKey);
                // uexEasemobLogin(obj.User.PhoneNumber, "123456");
              }
            })

        } else {
          //"该用户需要注册，请填写要设置的密码！"
          this.showPwd = true;
          this.api.showNoticeByToast(1, "该用户需要注册，请填写要设置的密码！");
        }
      })
  }

  registerUser() {
    if (this.password1 != this.password2) {
      this.api.showNoticeByToast(1, "两次密码不一致！");
      return false;
    }
    this.api.postBody("V1/ThirdRegister", {
      "PhoneNumber": this.mobile,
      "Password": this.password1,
      "Code": this.code,
      "Title": "未设置",
      "Type": this.params.Type,
      "Context": this.params.openid
    });




  }
  loading;
  private loginSucess(data) {

    if (data.Code === 0) {
      let obj = data.Data;
      //存入本地
      localStorage.setItem('Ticket', obj.LoginTicket);
      localStorage.setItem('User', JSON.stringify(obj.User));
    
      this.storage.set('firstIn', 2)
      //设置极光别名
      this.myJPushService.setAlias(obj.User.PushKey);
      this.navCtrl.push(TabsPage);
      this.loading.dismiss();
    } else {
      this.loading.dismiss();
      this.api.showTip(data.Msg);
    }
  }
  verifyCodeWait = false;
  vcodeName = "获取验证码";
  getvcode() {
    //判断手机是否可以注册
    if (this.mobile == null || this.mobile == "") {
      this.api.showNoticeByToast(1, "手机号不能为空");
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(this.mobile))) {
      this.api.showNoticeByToast(1, "请输入正确的格式手机号码！");
      return false;
    } else {
      this.url.CheckPhoneNumber(this.mobile)
        .then(data => {
          //console.log(data);
          if (data.Code == 2) {
            this.Getcode();//获取手机验证码
          } else {
            this.api.showNoticeByToast(1, data.Msg);
          }
        })
    }
  }
  vcodetime = 60;
  countDownTimer;
  countDown() {
    this.countDownTimer = setInterval(() => {
      this.vcodetime--;
      if (this.vcodetime === 0) {
        this.verifyCodeWait = false;
        this.vcodetime = 60;
        clearInterval(this.countDownTimer);
      }
    }, 1000)
  }

  ionViewWillUnload() {
    clearInterval(this.countDownTimer);
  }
  /**
 * 获取手机验证码
 */
  Getcode() {
    this.verifyCodeWait = true;
    this.countDown();
    this.url.getVcode(this.mobile, 3)
      .then(data => {
        if (data.Code == 0) {
          // this.api.showNoticeByToast(1, data.Msg);
        } else {
          this.api.showNoticeByToast(1, data.Msg);
        }
      })
      .catch(error => {

      })
  }

  chaPwd(index) {
    if (index == 2) {
      this.chakanPwd2 = !this.chakanPwd2;
    } else {
      this.chakanPwd = !this.chakanPwd;
    }
  }
}
